package com.turing.data.scripts.yhlc.filter

import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.common.util.Tools
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.data.engine.utils.RegexExtract

/**
 * @description: 银行理财 - 确认日期/截止日期
 * @author: mazhishen
 */

class ConfirmDate_30253_46_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){
        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        def relatedMap = dependParams.relatedBusiElements
        def relatedObject = new TaskElementVO.DrawResultVO()
        if(Tools.isNotEmpty(relatedMap.get("日期"))){
            relatedObject = relatedMap.get("日期").get(0)
        }
        if(relatedObject != null && !StringUtils.isEmpty(relatedObject.text)){
            trueValueSet.add(relatedObject as TaskElementVO.DrawResultVO)
            return trueValueSet
        }

        def REGEX_BACKUP = "应在"
        def REGEX_BACKUP_1 = "于"
        def REGEX_BACKUP_2 = "缴款截止时间"
        def REGEX_BACKUP_3 = "确认"

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    return resultVOs
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
//                            .replaceAll("-","")
//                drawResultVO.text = text
//                drawResultVO.businessValue = text
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                //往前10个字符搜索
                while( expandOffset < 10 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX_BACKUP || expandString =~ REGEX_BACKUP_1 ||
                            expandString =~ REGEX_BACKUP_2 || expandString =~ REGEX_BACKUP_3 )
                    {
                        println("ConfirmDate_drawResultVO="+drawResultVO)
                        drawResultVO.text = drawResultVO.text.replaceAll("-","")
                        drawResultVO.businessValue = drawResultVO.text

                        trueValueSet.add(drawResultVO)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        try{
            if(trueValueSet.size()==0){

                String REGEX = /((于|千|干).{0,5}|确认.{0,3})20.{1,10}日/
                println dependParams.JSONTextMap.size()
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )
                println("ConfirmDate_trueValueSet="+trueValueSet)

                if(trueValueSet.size()>0){
                    def textPathMap = dependParams.textPathMap
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)
                    def text = trueValueSet[0].text
                    def startOffset = text.indexOf("20")
                    def endOffset = 0
                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    return trueValueSet
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }

        //如果过滤后没有值，那么就默认返回模型给的第一个值
        if(trueValueSet.size() == 0 && resultVOs.size() > 0 && !StringUtils.isEmpty(resultVOs[0].text)){
            trueValueSet.add(resultVOs[0])
            return  trueValueSet
        }

//        if(trueValueSet.size() == 0){
//            def relatedMap = dependParams.relatedObjects
//            def relatedObject = relatedMap.get("日期")
//            if(relatedObject != null && !StringUtils.isEmpty(relatedObject.text)){
//                trueValueSet.add(relatedObject as TaskElementVO.DrawResultVO)
//                return trueValueSet
//            }
//        }

        List< TaskElementVO.DrawResultVO > defaultResultVOs = new ArrayList<>()
        TaskElementVO.DrawResultVO defaultResultVO = new TaskElementVO.DrawResultVO()
        defaultResultVO.text = ""
        defaultResultVOs.add(defaultResultVO)
        return defaultResultVOs

    }

}
